﻿using ft_module_parser;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ft_universal_parser
{
    class pv_fields
    {
        public List<pv_field_raw> pv_field = new List<pv_field_raw>();
        
        public void load(string file)
        {
            StreamReader sr = new StreamReader(file);
            while (sr.Peek() > -1)
            {
                string line = sr.ReadLine();
                if (!line.StartsWith("#"))
                {
                    string[] line2 = line.Split('.');
                    string[] line3 = line2[0].Split('_');

                    int pvid = int.Parse(line3[1]);

                    var curRawPv = pv_field.Where(c => c.pvId == pvid).FirstOrDefault();
                    if (curRawPv == null)
                    {
                        pv_field.Add(new pv_field_raw(pvid));
                        curRawPv = pv_field.Where(c => c.pvId == pvid).FirstOrDefault();
                        curRawPv.rawrows.Add(line);
                    }
                    else
                    {
                        curRawPv.rawrows.Add(line);
                    }
                }
            }
            sr.Close();
        }

        public void merge(string file, int id)
        {
            /*
            int id = 1;
            var test = pv_field.Where(c => c.pvId == id).FirstOrDefault();
            
            while (test != null)
            {
                id++;
                test = pv_field.Where(c => c.pvId == id).FirstOrDefault();
            }
            */

            //Console.WriteLine("found unused id at " + id);
            
            //bool easy = false, normal = false, hard = false, extreme = false;

            StreamReader sr = new StreamReader(file);
            while (sr.Peek() > -1)
            {
                string line = sr.ReadLine();
                if ((!line.StartsWith("#")) && (line.Count() != 0))
                {
                    string[] line2 = line.Split('.');
                    string[] line3 = line2[0].Split('_');

                    int pvid = int.Parse(line3[1]);

                    var curRawPv = pv_field.Where(c => c.pvId == id).FirstOrDefault();
                    if (curRawPv == null)
                    {
                        pv_field.Add(new pv_field_raw(id));
                        curRawPv = pv_field.Where(c => c.pvId == id).FirstOrDefault();

                        /*
                        var curdiff = added_fields.Where(c => c.pvid == id).First();

                        if (line.Contains("easy.script"))
                            curdiff.easy = true;
                        if (line.Contains("normal.script"))
                            curdiff.normal = true;
                        if (line.Contains("hard.script"))
                            curdiff.hard = true;
                        if (line.Contains("extreme.script"))
                            curdiff.extreme = true;

                        */

                        line = line.Remove(0, 6);
                        line = "pv_" + string.Format("{0:000}", id) + line;
                        curRawPv.rawrows.Add(line);

                    }
                    else
                    {
                        /*
                        var curdiff = added_fields.Where(c => c.pvid == id).First();

                        if (line.Contains("easy.script"))
                            curdiff.easy = true;
                        if (line.Contains("normal.script"))
                            curdiff.normal = true;
                        if (line.Contains("hard.script"))
                            curdiff.hard = true;
                        if (line.Contains("extreme.script"))
                            curdiff.extreme = true;
                        */

                        line = line.Remove(0, 6);
                        line = "pv_" + string.Format("{0:000}", id) + line;
                        curRawPv.rawrows.Add(line);
                    }
                }
            }
            sr.Close();
        }

        public void SaveTo(string s)
        {
            StreamWriter outp = new StreamWriter(s);

            var ordered = pv_field.OrderBy(c => c.pvId);

            foreach (var i in ordered)
            {
                foreach (var u in i.rawrows)
                {
                    outp.WriteLine(u);
                }
            }

            outp.Close();
        }
    }
}
